<?php
/**
 * Created by PhpStorm.
 * User: HP_USER
 * Date: 2019/1/7
 * Time: 10:35
 */

namespace app\user;


use app\common\Common;
use app\data\OriginalDate;
use think\Db;

class OriginalMember extends Base
{
    /**
     * 用户名字长度限制
     * @var array
     */
    private $userName = [2, 10];

    /**
     * 创始会员信息展示
     * @param $user_id
     * @return \think\response\Json
     */
    public function showOriginalMemberInfo($user_id)
    {
        $original = Common::getTableValue('drh_original_member', ['verify_status', 'recover_status'],
            ['user_id'], [$user_id]);
        if (empty($original) || $original['verify_status'] == 0) {
            return json(['code' => $this->f_code, 'msg' => '该用户不是创始会员或创始会员过期或正在等待审核']);
        }
        $data = new OriginalDate();
        $res = $data->showOriginalMemberInfo($user_id);
        $number = $data->getRecommendOriginalMemberNumber($user_id);
        $res['friend_num'] = $number;
        $res['time'] = time();
        return json([
            'code' => $this->t_code,
            'msg' => 'OK',
            'data' => $res
        ]);
    }

    /**
     * 申请创始会员
     * @param $user_id
     * @param $user_name
     * @param $card_number
     * @param $img_positive
     * @param $img_back
     * @param $img_signature
     * @param $integral
     * @return \think\response\Json
     */
    public function addOriginalMember($user_id, $user_name, $card_number, $img_positive, $img_back, $img_signature, $integral)
    {

        $res = $this->checkOriginal($user_id, $user_name, $card_number, $img_positive,
            $img_back, $img_signature, $integral);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }
        if (Common::checkTableFields('drh_original_member', 'user_id', $user_id) == $this->t_code) {
            return json(['code' => $this->f_code, 'msg' => '请勿重复申请/不具备申请资格']);
        }
        $user = new OriginalDate();
        $time = time();
        $res = $user->addOriginalMember($user_id, $user_name, $card_number, $time, $img_positive, $img_back, $img_signature, $integral);

        if (is_numeric($res)) {
            Common::addMsg($user_id, 0, $time, "你正在申请创始会员，请根据电子合同内容转账至对公账户:
        开户行：浙江民泰商业银行股份有限公司成都高新支行\r\n
        账号：583721434700015\r\n
        账号名称：成都大荣荟科技有限公司", '');
            return json([
                'code' => $this->t_code, 'msg' => '申请成功,请等待人工审核'
            ]);
        } else {
            return json([
                'code' => $this->f_code, 'msg' => '申请失败请稍后重试'
            ]);
        }
    }

    /**
     * 修改创始会员提交信息
     * @param $user_id
     * @param $user_name
     * @param $card_number
     * @param $img_positive
     * @param $img_back
     * @param $img_signature
     * @param $integral
     * @return \think\response\Json
     */
    public function editOriginalMember($user_id, $user_name, $card_number, $img_positive, $img_back, $img_signature, $integral)
    {
        $res = $this->checkOriginal($user_id, $user_name, $card_number, $img_positive,
            $img_back, $img_signature, $integral);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }
        if (Common::checkTableFields('drh_original_member', 'user_id', $user_id) == $this->f_code) {
            return json(['code' => $this->f_code, 'msg' => '不存在该创始会员']);
        }
        $user = new OriginalDate();
        $time = time();
        $res = $user->editOriginalMember($user_id, $user_name, $card_number, $time, $img_positive, $img_back, $img_signature, $integral);
        if (is_numeric($res)) {
            Common::addMsg($user_id, 0, $time, "你正在申请创始会员，请根据电子合同内容转账至对公账户:
        开户行：浙江民泰商业银行股份有限公司成都高新支行\r\n
        账号：583721434700015\r\n
        账号名称：成都大荣荟科技有限公司", '');
            return json([
                'code' => $this->t_code, 'msg' => '申请成功,请等待人工审核'
            ]);
        } else {
            return json([
                'code' => $this->f_code, 'msg' => '申请失败请稍后重试'
            ]);
        }
    }

    private function checkOriginal($user_id, $user_name, $card_number, $img_positive, $img_back, $img_signature, $integral, $type = 1)
    {
        if (Common::checkTableFields('drh_user_info', 'id', $user_id) == $this->f_code) {
            return ['code' => $this->f_code, 'msg' => '用户不存在'];
        }

        if (!Common::validateIdCard($card_number)) {
            return ['code' => $this->f_code, 'msg' => '身份证号码错误'];
        }
        if (!is_numeric($integral) || $integral < 0) {
            return ['code' => $this->f_code, 'msg' => '输入投股数错误'];
        }
        $len = mb_strlen($user_name);
        if ($len < $this->userName[0] || $len > $this->userName[1]) {
            return ['code' => $this->f_code, 'msg' => '请输入正确的姓名'];
        }
        $bool_positive = preg_match('/.*(\.png|\.jpg|\.jpeg|\.gif)$/', $img_positive);
        $bool_back = preg_match('/.*(\.png|\.jpg|\.jpeg|\.gif)$/', $img_back);
        $bool_signature = preg_match('/.*(\.png|\.jpg|\.jpeg|\.gif)$/', $img_signature);
        if ($bool_positive != 1 && $bool_back != 1 && $bool_signature != 1) {
            return ['code' => $this->f_code, 'msg' => '图片格式错误'];
        }
        if (mb_strlen($img_positive) > 200 || mb_strlen($img_back) > 200) {
            return ['code' => $this->f_code, 'msg' => '图片长度过长'];
        }
        if ($type != 1 && $type != 2) {
            return ['code' => $this->f_code, 'msg' => '请求类别错误'];
        }
        return ['code' => $this->t_code];
    }

    /**
     * 退出创始会员
     * @param $user_id
     * @param $user_name
     * @param $card_number
     * @param $img_positive
     * @param $img_back
     * @param $img_signature
     * @return \think\response\Json
     */
    public function quitOriginalMember($user_id, $user_name, $card_number = '511025198708014972',
                                       $img_positive = '1.jpg',
                                       $img_back = '1.jpg', $img_signature = '1.jpg')
    {
        $integral = 10;
        $res = $this->checkOriginal($user_id, $user_name, $card_number, $img_positive,
            $img_back, $img_signature, $integral);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }
        $data = new OriginalDate();
        $res = $data->getLastQuitOriginalApply($user_id);
        if (Common::checkTableFields('drh_bank_card', 'user_id', $user_id) == $this->f_code) {
            return json(['code' => $this->f_code, 'msg' => '请添加银行卡后在进行退出操作']);
        }
        $info = $data->checkOriginalMemberStatus($user_id);
        if (empty($info)) {
            return json(['code' => $this->f_code, 'msg' => '账号状态异常无法退出,请联系管理员']);
        }
        if (!empty($res) && $res['verify_status'] != 2) {
            return json(['code' => $this->f_code, 'msg' => '请勿重复提交']);
        }
        $time = time();
        $order_num = Common::getOrderNumber('q', 11, $time);
        $data = new OriginalDate();
        $res = $data->quitOriginalMember($order_num, $time, $user_id, json_encode($info),
            $user_name, $card_number, $img_positive,
            $img_back, $img_signature);
        if ($res) {
            return json(['code' => $this->t_code, 'msg' => '申请成功,请等待人工审核!']);
        } else {
            return json(['code' => $this->f_code, 'msg' => '审核失败,请联系管理员!']);
        }
    }

    /**
     * 追加创始会员股数
     * @param $user_id
     * @param $user_name
     * @param $card_number
     * @param $img_positive
     * @param $img_back
     * @param $img_signature
     * @param $integral
     * @return \think\response\Json
     */
    public function appendOriginalIntegral($user_id, $user_name, $card_number, $img_positive,
                                           $img_back, $img_signature, $integral)
    {
        $res = $this->checkOriginal($user_id, $user_name, $card_number, $img_positive,
            $img_back, $img_signature, $integral);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }
        $original_id = Common::getTableValue('drh_original_member', 'id',
            ['user_id', 'verify_status', 'recover_status'], [$user_id, 1, 0]);
        if (empty($original_id)) {
            return json(['code' => $this->f_code, 'msg' => '不存在该创始会员']);
        }
        $time = time();
        $order_num = Common::getOrderNumber('a', 11, $time);
        $data = new OriginalDate();
        $id = $data->appendOriginalIntegral($user_id, $original_id, $user_name, $card_number, $img_positive,
            $img_back, $img_signature, $integral, $time, $order_num);
        if (empty($id)) {
            return json(['code' => $this->f_code, 'msg' => '系统繁忙请稍后在试']);
        } else {
            Common::addMsg($user_id, 0, $time, "你正在追加创始会员股份，请根据电子合同内容转账至对公账户:
        开户行：浙江民泰商业银行股份有限公司成都高新支行\r\n
        账号：583721434700015\r\n
        账号名称：成都大荣荟科技有限公司", '');
            return json(['code' => $this->t_code, 'msg' => '追加成功请等待']);
        }
    }

    /**
     * 创始会员应急申请
     * @param $user_id
     * @param $user_name
     * @param $card_number
     * @param $img_positive
     * @param $img_back
     * @param $img_signature
     * @return \think\response\Json
     */
    public function originalPledgeIntegral($user_id, $user_name, $card_number = '511025198708014972',
                                           $img_positive = '1.jpg',
                                           $img_back = '1.jpg', $img_signature = '1.jpg')
    {
        $integral = 10;
        $res = $this->checkOriginal($user_id, $user_name, $card_number, $img_positive,
            $img_back, $img_signature, $integral);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }
        $original_id = Common::getTableValue('drh_original_member', 'id',
            ['user_id', 'verify_status', 'recover_status'], [$user_id, 1, 0]);
        if (empty($original_id)) {
            return json(['code' => $this->f_code, 'msg' => '不存在该创始会员']);
        }

        $time = time();
        $order_num = Common::getOrderNumber('z', 11, $time);
        $data = new OriginalDate();
        $res = $data->checkOriginalPledgeIntegral($original_id);
        if (!empty($res) && $res['verify_status'] == 0) {
            return json(['code' => $this->f_code, 'msg' => '请勿重复质押']);
        }
        $id = $data->originalPledgeIntegral($user_id, $original_id, $user_name, $card_number, $img_positive,
            $img_back, $img_signature, $time, $order_num);
        if (empty($id)) {
            return json(['code' => $this->f_code, 'msg' => '系统繁忙请稍后在试']);
        } else {
            return json(['code' => $this->t_code, 'msg' => '应急质押申请成功,请等待管理人员审核']);
        }
    }

    /**
     * 显示最近一条审核状态
     * @param $user_id
     * @return \think\response\Json
     */
    public function showPledgeIntegralStatus($user_id)
    {
        $original_id = Common::getTableValue('drh_original_member', 'id',
            ['user_id', 'verify_status', 'recover_status'], [$user_id, 1, 0]);
        if (empty($original_id)) {
            return json(['code' => $this->f_code, 'msg' => '不存在该创始会员']);
        }
        $data = new OriginalDate();
        $res = $data->checkOriginalPledgeIntegral($original_id);
//        $verify_status = empty($res) ? '' : $res['verify_status'];
        if (!empty($res)) {
            $res['msg'] = json_decode($res['msg']);
        }

        return json(['code' => $this->t_code, 'msg' => 'OK', 'data' => $res]);
    }

    /**
     * 创始会员应急赎回
     * @param $user_id
     * @param $user_name
     * @param string $card_number
     * @param string $img_positive
     * @param $p_order_num
     * @param string $img_back
     * @param string $img_signature
     * @return \think\response\Json
     */
    public function originalPledgeRansomIntegral($user_id, $user_name, $p_order_num, $card_number = '511025198708014972',
                                                 $img_positive = '1.jpg',
                                                 $img_back = '1.jpg', $img_signature = '1.jpg')
    {
        $integral = 10;
        $res = $this->checkOriginal($user_id, $user_name, $card_number, $img_positive,
            $img_back, $img_signature, $integral);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }
        $id = Common::getTableValue('drh_original_pledge_integral', 'id',
            ['user_id', 'order_num', 'verify_status', 'type_status'], [$user_id, $p_order_num, 1, 0]);
        if (empty($id)) {
            return json(['code' => $this->f_code, 'msg' => '应急申请不存在或者以逾期,请联系管理员']);
        }
        $original_id = Common::getTableValue('drh_original_member', 'id',
            ['user_id', 'verify_status', 'recover_status'], [$user_id, 1, 0]);
        if (empty($original_id)) {
            return json(['code' => $this->f_code, 'msg' => '不存在该创始会员']);
        }
        $data = new OriginalDate();
        $time = time();
        $bool = $data->checkPledgeOrder($user_id, $p_order_num);
        if (!empty($bool) && $bool['verify_status'] == 0) {
            return json(['code' => $this->f_code, 'msg' => '请填写正确的单号或请勿重复提交申请']);
        }
        if (!empty($bool)) {
            if ($time - $bool['end_time'] > 60 * 24 * 60 * 60) {//超过28天
                return json(['code' => $this->f_code, 'msg' => '应急质押以过期']);
            }
        }

        $order_num = Common::getOrderNumber('sh', 11, $time);
        $id = $data->originalPledgeRansomIntegral($user_id, $original_id, $user_name, $card_number, $img_positive,
            $img_back, $img_signature, $time, $order_num, $p_order_num);
        if (empty($id)) {
            return json(['code' => $this->f_code, 'msg' => '系统繁忙请稍后在试']);
        } else {
            Common::addMsg($user_id, 0, $time, "你正在赎回应急股份，请根据电子合同内容转账至对公账户:
        开户行：浙江民泰商业银行股份有限公司成都高新支行\r\n
        账号：583721434700015\r\n
        账号名称：成都大荣荟科技有限公司", '');
            return json(['code' => $this->t_code, 'msg' => '应急质押赎回申请成功,请等待管理人员审核']);
        }
    }

    /**
     * 赎回状态显示
     * @param $user_id
     * @return \think\response\Json
     */
    public function showPledgeRansomIntegralStatus($user_id)
    {
        $original_id = Common::getTableValue('drh_original_member', 'id',
            ['user_id', 'verify_status', 'recover_status'], [$user_id, 1, 0]);
        if (empty($original_id)) {
            return json(['code' => $this->f_code, 'msg' => '不存在该创始会员']);
        }
        $data = new OriginalDate();
        return json([
            'code' => $this->t_code,
            'msg' => 'OK',
            'data' => $data->getPledgeRansomIntegralStatus($user_id)
        ]);

    }

    /**
     * 创始会员推荐金额提现申请
     * @param $user_id
     * @param $user_name
     * @param $card_number
     * @param $img_positive
     * @param $img_back
     * @param $img_signature
     * @param $money
     * @return \think\response\Json
     */
    public function originalRecommendIntegral($user_id, $user_name, $card_number = '511025198708014972',
                                              $img_positive = '1.jpg', $money,
                                              $img_back = '1.jpg', $img_signature = '1.jpg')
    {
        $integral = 10;
        $res = $this->checkOriginal($user_id, $user_name, $card_number, $img_positive,
            $img_back, $img_signature, $integral);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }
        $original_id = Common::getTableValue('drh_original_member', ['id', 'recommend_integral'],
            ['user_id', 'verify_status', 'recover_status'], [$user_id, 1, 0]);
        if (empty($original_id)) {
            return json(['code' => $this->f_code, 'msg' => '不存在该创始会员']);
        }
        if ($original_id['recommend_integral'] < $money) {
            return json(['code' => $this->f_code, 'msg' => '申请金额超过拥有金额']);
        }
        $time = time();
        $order_num = Common::getOrderNumber('ow', 11, $time);
        $data = new OriginalDate();
        Db::startTrans();
        $id = $data->originalRecommendIntegral($user_id, $original_id['id'], $user_name, $card_number, $img_positive,
            $img_back, $img_signature, $time, $order_num, $money);
        $bool = $data->originalRecommendIntegralChange($user_id, $original_id['id'], $original_id['recommend_integral'] - $money);

        if (empty($id) || empty($bool)) {
            Db::rollback();
            return json(['code' => $this->f_code, 'msg' => '系统繁忙请稍后在试']);
        } else {
            Db::commit();
            return json(['code' => $this->t_code, 'msg' => '提现申请成功,请等待管理人员审核']);
        }
    }


    /**
     * 创始会员分红金额提现申请
     * @param $user_id
     * @param $user_name
     * @param $card_number
     * @param $img_positive
     * @param $img_back
     * @param $img_signature
     * @param $money
     * @return \think\response\Json
     */
    public function originalDividendIntegral($user_id, $user_name, $card_number = '511025198708014972',
                                              $img_positive = '1.jpg', $money,
                                              $img_back = '1.jpg', $img_signature = '1.jpg')
    {
        $integral = 10;
        $res = $this->checkOriginal($user_id, $user_name, $card_number, $img_positive,
            $img_back, $img_signature, $integral);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }
        $original_id = Common::getTableValue('drh_original_member', ['id', 'dividend_integral'],
            ['user_id', 'verify_status', 'recover_status'], [$user_id, 1, 0]);
        if (empty($original_id)) {
            return json(['code' => $this->f_code, 'msg' => '不存在该创始会员']);
        }
        if ($original_id['dividend_integral'] < $money) {
            return json(['code' => $this->f_code, 'msg' => '申请金额超过拥有金额']);
        }
        $time = time();
        $order_num = Common::getOrderNumber('dw', 11, $time);
        $data = new OriginalDate();
        Db::startTrans();
        $id = $data->originalDividendIntegral($user_id, $original_id['id'], $user_name, $card_number, $img_positive,
            $img_back, $img_signature, $time, $order_num, $money);
        $bool = $data->originalDividendIntegralChange($user_id, $original_id['id'], $original_id['dividend_integral'] - $money);

        if (empty($id) || empty($bool)) {
            Db::rollback();
            return json(['code' => $this->f_code, 'msg' => '系统繁忙请稍后在试']);
        } else {
            Db::commit();
            return json(['code' => $this->t_code, 'msg' => '提现申请成功,请等待管理人员审核']);
        }
    }
}